package com.ruoyi.framework.web.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.message.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @version :
 * @program: shipCargoEasyLink
 * @description:  短信发送类
 * @author: 周爱鸿
 * @create: 2020-08-04 15:58
 **/
@Component
@Slf4j
public class MessageService {

	@Value("${message.templateCode}")
	private String templateCode;

	@Value("${message.signName}")
	private String signName;


	@Autowired
	private RedisCache redisCache ;

	/**
	 * 获取登录验证码
	 * @param userType 用户类型（00系统用户  11 app用户）
	 * @param phone 手机号码
	 * @return
	 */
	public String getLoginVerificationCode(String phone , String userType){
		//生成6位验证码
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

		String verifyKey = "";
		if (UserConstants.APP_USER.equals(userType)){
			verifyKey =  UserConstants.APP_USER_MESSAGE_PRIFEX + phone;
		}
		redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		SendSmsResponse sendSmsResponse = null;
		try {
			sendSmsResponse = MessageUtil.sendSms(phone, signName, templateCode, "{\"code\":\""+verifyCode+"\"}");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("短信接口返回的数据----------------");
		log.info("Code=" + sendSmsResponse.toString());
		return sendSmsResponse.getCode()	;
	}
	/**
	 * app用户微信绑定手机号短信
	 * @param phone 手机号码
	 * @return
	 */
	public String getWxBingPhoneVerificationCode(String phone){
		//生成6位验证码
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

		String verifyKey = UserConstants.APP_USER_WXBINDPHONE_MESSAGE_PRIFEX + phone ;
		redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		SendSmsResponse sendSmsResponse = null;
		try {
			sendSmsResponse = MessageUtil.sendSms(phone, signName, templateCode, "{\"code\":\""+verifyCode+"\"}");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("短信接口返回的数据----------------");
		log.info("Code=" + JSONObject.toJSONString(sendSmsResponse));
		return sendSmsResponse.getCode()	;
	}
	/**
	 * app用户注册手机号短信
	 * @param phone 手机号码
	 * @return
	 */
	public String getLogonVerificationCode(String phone){
		//生成6位验证码
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

		String verifyKey = UserConstants.APP_USER_LOGON_MESSAGE_PRIFEX + phone ;
		redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		SendSmsResponse sendSmsResponse = null;
		try {
			sendSmsResponse = MessageUtil.sendSms(phone, signName, templateCode, "{\"code\":\""+verifyCode+"\"}");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("短信接口返回的数据----------------");
		log.info("Code=" + JSONObject.toJSONString(sendSmsResponse));
		return sendSmsResponse.getCode()	;
	}
	/**
	 * app用户注册手机号短信
	 * @param phone 手机号码
	 * @return
	 */
	public String getForgetpasswordMessage(String phone){
		//生成6位验证码
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

		String verifyKey = UserConstants.APP_USER_FORGETPASSWORD_MESSAGE_PRIFEX + phone ;
		redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		SendSmsResponse sendSmsResponse = null;
		try {
			sendSmsResponse = MessageUtil.sendSms(phone, signName, templateCode, "{\"code\":\""+verifyCode+"\"}");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("短信接口返回的数据----------------");
		log.info("Code=" + JSONObject.toJSONString(sendSmsResponse));
		return sendSmsResponse.getCode()	;
	}
	/**
	 * 更改手机号验证码
	 * @param phone 手机号码
	 * @return
	 */
	public String getChangePhoneMessage(String phone){
		//生成6位验证码
		String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

		String verifyKey = UserConstants.APP_USER_CHANGE_PASSWORD + phone ;
		redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		SendSmsResponse sendSmsResponse = null;
		try {
			sendSmsResponse = MessageUtil.sendSms(phone, signName, templateCode, "{\"code\":\""+verifyCode+"\"}");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		log.info("短信接口返回的数据----------------");
		log.info("Code=" + JSONObject.toJSONString(sendSmsResponse));
		return sendSmsResponse.getCode()	;
	}

}
